/** @file types.inc
 * Data types definition.
 * This file holds all the type definitions and defines, relatade to data types, used in the software.
 *
 * Copyright (C) 2007 
 *   Renan Mendes Carvalho <aitherios@gmail.com>
 *   Pedro Marinho <pedro7x@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TYPES_INC

#define _TYPES_INC

/** defines a 1 byte type */
#define u1 unsigned char
/** defines a 2 bytes type */
#define u2 unsigned short int
/** defines a 4 bytes type */
#define u4 unsigned int
/** defines a 1 byte type */
#define s1 signed char
/** defines a 2 bytes type */
#define s2 signed short int
/** defines a 4 bytes type */
#define s4 signed int


/**
 * Constant pool entries.
 */
typedef struct cp_info 
{
  /**
   * Indicates the kind of cp_info entry.
   * The info array variation depends on the value of tag. The following values are the possible ones:
   * CONSTANT_Class    7 
   * CONSTANT_Fieldref    9 
   * CONSTANT_Methodref    10 
   * CONSTANT_InterfaceMethodref    11 
   * CONSTANT_String    8 
   * CONSTANT_Integer    3 
   * CONSTANT_Float    4 
   * CONSTANT_Long    5 
   * CONSTANT_Double    6 
   * CONSTANT_NameAndType    12 
   * CONSTANT_Utf8    1
   */
  u1 tag; /**< specifies the type of cp_info entry */
  u1* info; /**< depends on the kind of tag */
  u1* utf8info; /**< utf8 case */
} cp_info;

/* constants defined for tag element of cp_info structure */
#define CONSTANT_Class    7 
#define CONSTANT_Fieldref    9 
#define CONSTANT_Methodref    10 
#define CONSTANT_InterfaceMethodref    11 
#define CONSTANT_String    8 
#define CONSTANT_Integer    3 
#define CONSTANT_Float    4 
#define CONSTANT_Long    5 
#define CONSTANT_Double    6 
#define CONSTANT_NameAndType    12 
#define CONSTANT_Utf8    1 

/* access flags definitions */
#define ACC_PUBLIC    0x0001    
#define ACC_PRIVATE    0x0002
#define ACC_PROTECTED    0x0004
#define ACC_STATIC    0x0008
#define ACC_FINAL    0x0010
#define ACC_SUPER    0x0020
#define ACC_SYNCHRONIZED    0x0020
#define ACC_VOLATILE    0x0040
#define ACC_TRANSIENT    0x0080
#define ACC_NATIVE    0x0100   
#define ACC_INTERFACE    0x0200
#define ACC_ABSTRACT    0x0400
#define ACC_STRICT    0x0800

/**
 * AttributeInfo structure
 */
typedef struct attribute_info {
  u2 attribute_name_index; /**< name, valid CONSTANT_Utf8_info index to the constant pool table */
  u4 attribute_length; /**< length of the subsequent information in bytes */
  u1* info; /**< varies, depend on particuliarities of each attribute type*/
} attribute_info;

/**
 * FieldInfo structure, fields[] entries.
 */
typedef struct field_info {
  /**
   * Access control flags to fields.
   * Possible, non excludent, values:
   * ACC_PUBLIC    0x0001    Declared public; may be accessed from outside its package. 
   * ACC_PRIVATE    0x0002    Declared private; usable only within the defining class. 
   * ACC_PROTECTED    0x0004    Declared protected; may be accessed within subclasses. 
   * ACC_STATIC    0x0008    Declared static. 
   * ACC_FINAL    0x0010    Declared final; no further assignment after initialization. 
   * ACC_VOLATILE    0x0040    Declared volatile; cannot be cached. 
   * ACC_TRANSIENT    0x0080    Declared transient; not written or read by a persistent object manager.
   */
  u2 access_flags;
  u2 name_index; /**< name, valid CONSTANT_Utf8_info index to the constant pool table */
  u2 descriptor_index; /**< field descriptor, valid CONSTANT_Utf8_info index to the constant pool table */
  u2 attributes_count; /**< number of aditional attributes in this field */
  attribute_info* attributes; /**< attribute table */
} field_info;

/**
 * MethodInfo structure.
 */
typedef struct method_info {
  /**
   * Access control to methods.
   * Possible, non excludent, values:
   * ACC_PUBLIC    0x0001    Declared public; may be accessed from outside its package. 
   * ACC_PRIVATE    0x0002    Declared private; accessible only within the defining class. 
   * ACC_PROTECTED    0x0004    Declared protected; may be accessed within subclasses. 
   * ACC_STATIC    0x0008    Declared static. 
   * ACC_FINAL    0x0010    Declared final; may not be overridden. 
   * ACC_SYNCHRONIZED    0x0020    Declared synchronized; invocation is wrapped in a monitor lock. 
   * ACC_NATIVE    0x0100    Declared native; implemented in a language other than Java. 
   * ACC_ABSTRACT    0x0400    Declared abstract; no implementation is provided. 
   * ACC_STRICT    0x0800    Declared strictfp; floating-point mode is FP-strict
   */
  u2 access_flags;
  u2 name_index; /**< index to the constant pool table, CONSTANT_Utf8_info */
  u2 descriptor_index; /**< index to the constant pool table, CONSTANT_Utf8_info */
  u2 attributes_count; /**< number of aditional attributes */
  /**
   * Attributes table.
   * The only attributes defined by this specification as appearing in the attributes table of a ClassFile structure 
   * are the SourceFile attribute and the Deprecated attribute.
   */
  attribute_info* attributes;
} method_info;

/**
 * Represents the ClassFile structure. 
 * As seen in The JavaTM Virtual Machine Specification Second Edition, chapter 4.
 */
typedef struct class_file
{
  u4 magic; /**< class file format identifier, 0xCAFEBABE */
  u2 minor_version; /**< minor version of class file, version = major.minor */
  u2 major_version; /**< major version of class file, version = major.minor */
  u2 constant_pool_count; /**< number of entries in the constant_pool table plus 1 */
  cp_info* constant_pool; /**< table of structures representing string constants */
  /**
   * Access permissions and properties.
   * These are the possible flag values:
   * ACC_PUBLIC    0x0001    Declared public; may be accessed from outside its package. 
   * ACC_FINAL    0x0010    Declared final; no subclasses allowed. 
   * ACC_SUPER    0x0020    Treat superclass methods specially when invoked by the invokespecial instruction. 
   * ACC_INTERFACE    0x0200    Is an interface, not a class. 
   * ACC_ABSTRACT    0x0400    Declared abstract; may not be instantiated.
   */
  u2 access_flags;
  u2 this_class; /**< constant_pool entry, represents the class ou interface defined in the class file */
  u2 super_class; /**< index to the constant_pool table, father class */
  u2 interfaces_count; /**< number of direct superinterfaces of this class or interface */
  u2* interfaces; /**< index to constant pool table, direct superinterface in the left-right order */
  u2 fields_count; /**< number of field_info structures in the fields table */
  field_info* fields; /**< field_info table, description of fields */
  u2 methods_count; /**< number of method_info structures in the methods table */
  method_info* methods; /**< method_info table, description of methods */
  u2 attributes_count; /**< number of attribute_info structures in attributes table */
  attribute_info* attributes; /**< attribute_info table, elements concerning only the ClassFile */
} class_file;

/**
 * A generic stack structure.
 * Used mainly for runtime areas as heap, method area and java stack.
 */
typedef struct stack
{
  void* bp; /**<base pointer*/
  void* tp; /**<top pointer*/
  void* sp; /**<stack pointer*/
}stack;

/**
 * Represents the generic and thread specific Java Runtime areas.
 */
typedef struct th_runtime_area
{
  stack method_area;/**< method_area, class*/
  stack heap; /**<heap*/
  stack java; /**<java stack area*/
  stack pc_reg; /**<pc registers*/
  void * pc; /**<temporary pc*/
  stack native_methods;/**< native methods*/
}th_runtime_area;

/**
 * Represents an instance of a constructed object.
 */
typedef struct instance
{
  class_file * cf; /**< class reference */
  void * param; /**< parameter stack */
}instance;

#endif
